package xworker.gluon.glisten.control;

import com.gluonhq.charm.glisten.control.DropdownButton;
import javafx.scene.control.MenuItem;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.control.ControlActions;
import xworker.javafx.util.JavaFXUtils;

public class DropdownButtonActions {
    static{
        PropertyFactory.regist(DropdownButton.class, "selectedItemProperty", o -> {
            DropdownButton obj = (DropdownButton) o;
            return obj.selectedItemProperty();
        });
    }

    public static void init(DropdownButton node, Thing thing, ActionContext actionContext) {
        ControlActions.init(node, thing, actionContext);

        MenuItem selectedItem = JavaFXUtils.getObject(thing, "selectedItem", actionContext);
        if (selectedItem != null) {
            node.setSelectedItem(selectedItem);
        }

    }

    public static DropdownButton create(ActionContext actionContext) {
        Thing self = actionContext.getObject("self");
        DropdownButton node = new DropdownButton();
        init(node, self, actionContext);

        actionContext.peek().put("parent", node);
        for (Thing child : self.getChilds()) {
            child.doAction("create", actionContext);
        }

        actionContext.g().put(self.getMetadata().getName(), node);
        return node;
    }

    public static void createMenuItems(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        DropdownButton parent = actionContext.getObject("parent");

        for(Thing child : self.getChilds()){
            Object obj = child.doAction("create", actionContext);
            if(obj instanceof MenuItem){
                parent.getItems().add((MenuItem) obj);
            }
        }
    }

}